from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from datetime import datetime
from selenium.webdriver.common.keys import Keys
class Base():

    def __init__(self, driver):
        self.driver = driver
        
    def get_url(self, url):
        self.driver.get(url)
        self.driver.find_element().send_keys()

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc))
            return self.driver.find_element(*loc)

        except Exception as e:
            data_time = datetime.now().strftime('%Y%m%d %H%M%S')
            self.driver.save_screenshot(f'error_{data_time}.png')
            print('未定位到元素', e)

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    def click(self, loc):
        self.find_element(loc).click()
    
    def duanyan(self, loc):
        return self.find_element(loc).text
    

    def keys(self, loc):
        self.find_element(loc).send_keys(Keys.ENTER)

    #封装标题方法
    def tatile(self):
        return self.driver.title





    

    